var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session')

/*router*/
var indexRouter = require('./routes/index');

var CommonRouter = require('./routes/CommonRouter');
var StaffRouter = require('./routes/StaffRouter');
var AdminRouter = require('./routes/AdminRouter');





var app = express();

/*let busboy = require('connect-busboy');
app.use(busboy());*/
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
``
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());

app.use(cookieParser('session'));


app.use(session({
  secret: 'identifierID',
  name: 'identifierID',
  saveUninitialized: false,
  resave: false,
  unset: 'destroy',
  maxAge: 24 * 60 * 60 * 1000 * 7,
  rolling:true,

  cookie: { maxAge: 24 * 60 * 60 * 1000 * 7 },
  /*    store: new RedisStore({
          client: redisClient,
          ttl: 24 * 60 * 60,
          prefix: 'node-session'
      })*/
}));




//app.use(express.static("./node_modules"));
app.use(express.static(path.join(__dirname, '/')));

/*各种router和路径捆绑起来*/
app.use('/', indexRouter);
app.use('/common',CommonRouter);
app.use('/staff',StaffRouter);
app.use('/admin',AdminRouter);






// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');

});




module.exports = app
